import cv2
import numpy as np


raw = cv2.imread(r'C:\_AAA\BlueNet\color_seg/a-template.png', cv2.IMREAD_UNCHANGED)  # 读取bgra, 保留png的alpha通道

mask = np.zeros(raw.shape[:2], dtype=np.uint8)
mask[:] = 255

im_base = raw[:,:,:3].copy()
im_base[:,:,0] = 0
im_base[:,:,1] = 255
im_base[:,:,0] = 0


def blend(bgr, mask):
    a = 0.5
    m = mask / 255
    # bgr[:,:,0] = a * bgr[:,:,0] + (1-a) * m  # blending
    bgr[:,:,1] = a * bgr[:,:,1] + (1-a) * (im_base[:,:,1] * m)  # blending
    # bgr[:,:,2] = a * bgr[:,:,2] + (1-a) * m  # blending


def show():
    a = 0.5
    im_to_show = raw.copy()
    blend(im_to_show, mask)
    
    cv2.imshow('raw', im_to_show)
    cv2.imshow('b', raw[:,:,0])
    cv2.imshow('g', raw[:,:,1])
    cv2.imshow('r', raw[:,:,2])
    cv2.imshow('mask', mask)


def draw_mask(event, x, y, flags, params):
    print(event, x, y, flags, params)
    if flags & cv2.EVENT_FLAG_LBUTTON:
        cv2.circle(mask, (x,y), 10, [0,0,0], -1)
        # raw[:,:,3] = mask
    elif flags & cv2.EVENT_FLAG_RBUTTON:
        cv2.circle(mask, (x,y), 10, [255,255,255], -1)
        # raw[:,:,3] = mask
    
    show()
    # cv2.imwrite('alpha.png', raw)


show()
cv2.setMouseCallback('mask', draw_mask)


if __name__ == '__main__':
    while True:
        if cv2.waitKey(10) & 0xFF == 27:
            exit(0)





